﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
namespace PPFinance.Common
{
    public class ControlTree
    {
        public static T FindControl<T>(object parentControl, string controlName)
        {
            findedControl = null;
            find(parentControl, controlName);
            return (T)findedControl;
        }
        public static T FindControl<T>(string controlName)
        {
            return FindControl<T>(Application.Current.RootVisual, controlName);
        }
        static object findedControl = null;
        static private void find(object control, string controlName)
        {

            if (control is Border)
            {
                var con = control as Border;
                if (con.Name == controlName)
                {
                    findedControl = con;
                    return;
                }
                else
                {
                    if (con.Child != null)
                    {
                        find(con.Child, controlName);
                    }
                    else
                    {
                        return;
                    }
                }
            }
            if (control is UserControl || control is ContentControl)
            {
                //ContentControl 具有Content
                dynamic con = null;
                if (control is UserControl) con = control as UserControl;
                if (control is ContentControl) con = control as ContentControl;
                if (con.Name == controlName)
                {
                    findedControl = con;
                    return;
                }
                else
                {
                    if (con.Content != null)
                    {
                        find(con.Content, controlName);
                    }
                    else
                    {
                        return;
                    }
                }

            }
            if (control is Panel)
            {
                //Panel 具有Children
                var con = control as Panel;
                if (con.Name == controlName)
                {
                    findedControl = con;
                    return;
                }
                else
                {
                    foreach (var item in con.Children)
                    {
                        if (item != null)
                        {
                            find(item, controlName);
                        }
                        else
                        {
                            return;
                        }
                    }
                }
            }
            if (control is Control)
            {
                var con = control as Control;
                if (con.Name == controlName)
                {
                    findedControl = con;
                    return;
                }
            }
            return;
        }
    }
}
